package zmee.pmpt.formplugin;

import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.coderule.api.CodeRuleInfo;
import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.form.operate.FormOperate;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.coderule.CodeRuleServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

/**
 * @Author: zyj
 * @Date: 2023/8/4 15:53
 * @Description: 项目计划达成率: 点击保存数据按钮保存单据
 */
public class ProplanreacSaveBillPlugin extends AbstractBillPlugIn{

    @Override
    public void beforeDoOperation(BeforeDoOperationEventArgs args) {
        super.beforeDoOperation(args);
        FormOperate source = (FormOperate) args.getSource();
        String operateKey = source.getOperateKey();
        if ("savedata".equals(operateKey)) {
            DynamicObject proPlanReac = this.getModel().getDataEntity(true);
            // 获取数据明细单据体
            DynamicObjectCollection entryentity = proPlanReac.getDynamicObjectCollection("entryentity");
            if (entryentity == null || entryentity.size() == 0) {
                this.getView().showTipNotification("数据明细为空！请先查询后在保存单据！");
                args.setCancel(true);
            }
        }
    }

    @Override
    public void afterDoOperation(AfterDoOperationEventArgs e) {
        super.afterDoOperation(e);
        String operateKey = e.getOperateKey();
        if ("savedata".equals(operateKey)) {
            // 如果是保存数据
            // 获取单据对象
            DynamicObject proPlanReac = this.getModel().getDataEntity(true);
            // 获取数据明细单据体
            DynamicObjectCollection entryentities = proPlanReac.getDynamicObjectCollection("entryentity");
            // 创建一张新的项目计划达成率单
            DynamicObject proplanrate = BusinessDataServiceHelper.newDynamicObject("zmee_pmpt_proplanrate");
            // 项目计划达成率单的数据明细单据体
            DynamicObjectCollection datadetail = proplanrate.getDynamicObjectCollection("entryentity");
            // 将本单数据明细单据体赋值到项目计划达成率单的数据明细单据体
            for (DynamicObject entryentity : entryentities) {
                DynamicObject addNew = datadetail.addNew();
                addNew.set("project", entryentity.getDynamicObject("project"));
                addNew.set("planend", entryentity.get("planend"));
                addNew.set("ontimeend", entryentity.get("ontimeend"));
                addNew.set("overtimeend", entryentity.get("overtimeend"));
                addNew.set("totalend", entryentity.get("totalend"));
                addNew.set("overtimenotend", entryentity.get("overtimenotend"));
                addNew.set("ontimeendrate", entryentity.get("ontimeendrate"));
                addNew.set("overtimeendrate", entryentity.get("overtimeendrate"));
                addNew.set("endrate", entryentity.get("endrate"));
            }
            // 调用自动编码规则
            CodeRuleInfo codeRule = CodeRuleServiceHelper.getCodeRule(proplanrate.getDataEntityType().getName(), proplanrate, null);
            // 获取单据编码
            String number = CodeRuleServiceHelper.getNumber(codeRule, proplanrate);
            proplanrate.set("billno",number);
            // 设置单据状态
            proplanrate.set("billstatus", "A");
            // 获取当前用户id
            long currUserId = RequestContext.get().getCurrUserId();
            // 获取当前用户所在组织id
            long orgId = RequestContext.get().getOrgId();
            // 创建人
            proplanrate.set("creator", currUserId);
            // 组织
            proplanrate.set("org", orgId);
            // 数据范围-组织
            proplanrate.set("orgs", getDynamicObjectCollection("orgs"));
            // 数据范围-项目
            proplanrate.set("projects", getDynamicObjectCollection("projects"));
            // 数据范围-任务来源
            proplanrate.set("tasksource", getDynamicObjectCollection("tasksource"));
            // 数据范围-管控级别
            proplanrate.set("controllevels", getDynamicObjectCollection("controllevels"));
            // 数据范围-任务类型
            proplanrate.set("tasktypes", getDynamicObjectCollection("tasktypes"));
            Object thisweek = this.getModel().getValue("thisweek");
            proplanrate.set("thisweek", thisweek);
            Object thismonth = this.getModel().getValue("thismonth");
            proplanrate.set("thismonth", thismonth);
            Object threemonth = this.getModel().getValue("threemonth");
            proplanrate.set("threemonth", threemonth);
            proplanrate.set("daterange_startdate", this.getModel().getValue("daterange_startdate"));
            proplanrate.set("daterange_enddate", this.getModel().getValue("daterange_enddate"));
            SaveServiceHelper.save(new DynamicObject[]{proplanrate});
        }
    }

    /**
     * 多选基础资料赋值
     * @param identifying 多选基础资料标识
     * @return 多选基础资料集合，用于存放多选基础资料
     */
    public DynamicObjectCollection getDynamicObjectCollection(String identifying) {
        // 获取单据对象
        DynamicObject dataEntity = this.getModel().getDataEntity(true);
        // 获取多选基础资料
        DynamicObjectCollection dynamicObjectCollection = dataEntity.getDynamicObjectCollection(identifying);
        // 存放多选基础资料
        DynamicObjectCollection dynamicObjects = new DynamicObjectCollection();
        for (DynamicObject dynamicObject : dynamicObjectCollection) {
            // 获取基础资料
            Object fbasedataid = dynamicObject.get("fbasedataid");
            // 创建一个新的多选基础资料行
            DynamicObject tempDyObj = new DynamicObject(dynamicObjectCollection.getDynamicObjectType());
            tempDyObj.set("fbasedataid", fbasedataid);
            dynamicObjects.add(tempDyObj);
        }
        return dynamicObjects;
    }

}
